import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.text.*;

import java.lang.*;
import java.util.*;
import java.applet.*;

public class Affichage extends JApplet implements ActionListener, MouseListener, KeyListener{
	private static final long serialVersionUID = 3242545489455555416L; // serializable
	private JTextArea t, descr, research;
	private JScrollPane scroll, scrolld, scrollr;
	private JButton b;
	private JRadioButton cb1, cb2, cb3, cb4;
	private Action selectLine;
	private Container cp = getContentPane();
	
	ArrayList<BouteilleNoHerit> bou;
	ArrayList<BrasserieNoHerit> bra;
	ArrayList<LieuOrigineNoHerit> lon;
	ArrayList<BreuvageNoHerit> bre;
	
	
	private Action getAction(String name) {
	Action action = null;
    Action[] actions = t.getActions();
        for (int i = 0; i < actions.length; i++) {
            if (name.equals( actions[i].getValue(Action.NAME).toString())) {
                action = actions[i];
                break;
            }
        }

        return action;
    }

	public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}

	public void mouseClicked(MouseEvent e) {
		if (SwingUtilities.isLeftMouseButton(e)  && e.getClickCount() == 1) {
            selectLine.actionPerformed(null);
        }
        
        for(int i = 0; i <bre.size() ; i++) {
			if(t.getSelectedText().contains(bre.get(i).getNomBierre())) {
				descr.setText((bre.get(i)).toAffiche());
			}
		}	
    }
    
    public void keyPressed(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}
	
	public void keyTyped(KeyEvent e) {
/*		String rech;
		if(Character.isLetter(e.getKeyChar()) || Character.isWhitespace(e.getKeyChar())) {
			rech = (research.getText() + e.getKeyChar()).toLowerCase();
		}
		else {
			rech = research.getText().toLowerCase();
		}
			
		clearText(t);
		for(int i = 0; i < 100 ; i++) {
			if((bre.get(i).getNomBierre()).contains(rech)) {
				if(cb1.isSelected()) {
					draw((bou.get(i)).toAffiche());
				}
				else if(cb2.isSelected()) {
					draw((bra.get(i)).toAffiche());
				}
				else if(cb3.isSelected()) {
					draw((lon.get(i)).toAffiche());
				}
				else if(cb4.isSelected()) {
					draw((bre.get(i)).toAffiche());
				}
			}	
			if((lon.get(i).getNomLieuOrigine()).contains(rech) || (lon.get(i).getPaysAppartenance()).contains(rech)) {
				if(cb1.isSelected()) {
					draw((bou.get(i)).toAffiche());
				}
				else if(cb2.isSelected()) {
					draw((bra.get(i)).toAffiche());
				}
				else if(cb3.isSelected()) {
					draw((lon.get(i)).toAffiche());
				}
				else if(cb4.isSelected()) {
					draw((bre.get(i)).toAffiche());
				}
			}	
			if((bra.get(i).getBrasserie()).contains(rech)) {
				/*if(cb1.isSelected()) {
					draw((bou.get(i)).toAffiche());
				}
				else if(cb2.isSelected()) {
					draw((bra.get(i)).toAffiche());
				}
				/*else if(cb3.isSelected()) {
					draw((lon.get(i)).toAffiche());
				}
				else if(cb4.isSelected()) {
					draw((bre.get(i)).toAffiche());
				}
			}	
		}*/
	}
	
	public void init(){
	setFocusable(true);	
	ButtonGroup bg=new ButtonGroup(); 
	b = new JButton("Gestion des Breuvages");
	
	t = new JTextArea();
	descr = new JTextArea();	
	research = new JTextArea();
	
	/*ufbou=new UseFile<BouteilleNoHerit>("bouteille");
	ufbra=new UseFile<BrasserieNoHerit>("brasserie");
	uflon=new UseFile<LieuOrigineNoHerit>("lieuorigine");
	ufbre=new UseFile<BreuvageNoHerit>("beer");*/
	
	UseFile<BouteilleNoHerit> ufbou=new UseFile<BouteilleNoHerit>("bouteille");
	UseFile<BrasserieNoHerit> ufbra=new UseFile<BrasserieNoHerit>("brasserie");
	UseFile<LieuOrigineNoHerit> uflon=new UseFile<LieuOrigineNoHerit>("lieuorigine");
	UseFile<BreuvageNoHerit> ufbre=new UseFile<BreuvageNoHerit>("beer");
	
	bou = ufbou.loadBouteille();
	bra=ufbra.loadBrasserie();
	lon=uflon.loadLieuOrigine();
	bre=ufbre.loadBreuvage();
	
	
	//bouton
	cb1 = new JRadioButton("Bouteilles");
	cb2 = new JRadioButton("Brasseries");
	cb3 = new JRadioButton("Lieu origine");
	cb4 = new JRadioButton("Breuvages", true);

	bg.add(cb1);
	bg.add(cb2);
	bg.add(cb3);
	bg.add(cb4);
	
	b.addActionListener(this);
	t.addMouseListener(this);
	research.addKeyListener(this);
	
	cb1.addActionListener(this);	
	cb2.addActionListener(this);
	cb3.addActionListener(this);
	cb4.addActionListener(this);
	
	cp.setLayout(null);
	
	t.setEditable(false);
	descr.setEditable(false);
	
	b.setBounds(420, 230, 250, 50);
	
	cb1.setBounds(40, 400, 110, 20); // x, y, sizex, sizey
	cb2.setBounds(150, 400, 110, 20);
	cb3.setBounds(260, 400, 110, 20);
	cb4.setBounds(370, 400, 110, 20);
	
	scroll = new JScrollPane(t);
	scroll.setBounds(10, 10, 400, 350);
	
	scrolld = new JScrollPane(descr);
	scrolld.setBounds(420, 10, 370, 200);
	
	scrollr = new JScrollPane(research);
	scrollr.setBounds(60, 450, 250, 45);	
		
	cp.add(b);
	cp.add(scroll);
	cp.add(scrolld);
	cp.add(cb1);
	cp.add(cb2);
	cp.add(cb3);
	cp.add(cb4);
	cp.add(scrollr);
	
	selectLine = getAction(DefaultEditorKit.selectLineAction);
	//showToutes(cb4);
	/*for(int i = 0; i < bre.size(); i++) {
		draw((bre.get(i)).toAffiche());
		}*/
}
	
	//
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == cb1) {
			showBouteille(cb1);
			clearText(descr);
		}
		else if(e.getSource() == cb2) {
			showBrasserie(cb2);
			clearText(descr);
		}
		else if(e.getSource() == cb3) {
			showLieu(cb3);
			clearText(descr);
		}
		else if(e.getSource() == cb4){
			showToutes(cb4);
			clearText(research);
		}
		else if(e.getSource() == b) {
			new Panel().gestion();
			if(cb1.isSelected()) { 
				showBouteille(cb1);
				clearText(descr);
			}
			else if(cb2.isSelected()) {
				showBrasserie(cb2);
				clearText(descr);
			}
			else if(cb3.isSelected()) {
				showLieu(cb3);
				clearText(descr);
			}
			else {
				showToutes(cb4);
				clearText(research);
			}
		}
}

	private void showBouteille(JRadioButton b) {
		clearText(t);
		if(b.isSelected()) {
			for(int i = 0; i < bou.size(); i++) {
				draw((bou.get(i)).toAffiche());
			}
		}
	}
	
	private void showBrasserie(JRadioButton b) {
		clearText(t);
		if(b.isSelected()) {
			for(int i = 0; i < bra.size(); i++) {
				draw((bra.get(i)).toAffiche());
			}
		}
	}
	
	private void showLieu(JRadioButton b) {
		clearText(t);
		if(b.isSelected()) {
			for(int i = 0; i < lon.size(); i++) {
				draw((lon.get(i)).toAffiche());
			}
		}
	}
	
	private void showToutes(JRadioButton b) {
		clearText(t);
		if(b.isSelected()) {
			for(int i = 0; i < bre.size(); i++) {
				draw((bre.get(i)).getNomBierre());
			}
		}
	}
	
	
	private void clearText(JTextArea textArea) {
		textArea.setText("");
	}
	
	private void draw(String s) {
		t.append(s + "\n");
	}
	
}
